package com.oa.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.oa.bean.User;
import com.oa.service.impl.PersonalServiceImpl;

@Controller
public class PersonalController {

	@Resource
	PersonalServiceImpl personalServiceImpl;
	
	@RequestMapping("lookPersonalInfo")
	public String lookPersonalInfo(HttpSession session,Model model){
		int uid = (int) session.getAttribute("uid"); 
		System.out.println(uid);
		User user = personalServiceImpl.getUserById(uid);
		model.addAttribute("userInfo", user);
		return "Template/seeInformation";
	}
	
	@RequestMapping("getUserRoleSection")
	@ResponseBody
	public Map<String,Object> getUserRoleSection(Integer uid){
		Map<String,Object>map = personalServiceImpl.getUserSectionRole(uid);
		return map;
	}
	
	@RequestMapping("getUserList")
	@ResponseBody
	public Map<String,Object> getUserList(Integer currentPage,String username,String uname){
		System.out.println("username++++++++++++++"+username);
		System.out.println("uname++++++++++++"+uname);
		Map<String,Object>map = new HashMap<>();
		map.put("currentPage", currentPage);
		map.put("username", username);
		map.put("uname", uname);
		map = personalServiceImpl.getUserList(map);
		return map;
	}
	
	@RequestMapping("getRoleSectionList")
	@ResponseBody
	public Map<String,Object> getRoleSectionList(){
		Map<String,Object> map = personalServiceImpl.getSectionRole();
		return map;
	}
	
	@RequestMapping("addUser")
	public String addUser(User user,MultipartFile file){
		System.out.println(user);
		System.out.println(file);
		if(!"".equals(file.getOriginalFilename())){
			String fileName = file.getOriginalFilename();
			String prefix = fileName.substring(fileName.lastIndexOf("."));
			Date date = new Date();
			String newFileName = date.getTime()+prefix;
			String uploadPath="C:\\Users\\longer\\git\\OAXiTongGuanLi\\src\\main\\webapp\\head-portrait\\"+newFileName;//设置上传路径
			System.out.println("上传路径"+uploadPath);
			try {
				file.transferTo(new File(uploadPath));
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			user.setUhead(newFileName);
		}
        ByteSource credentialsSalt = ByteSource.Util.bytes(user.getUsername());
		SimpleHash hash = new SimpleHash("MD5","123456",credentialsSalt,1024);
		user.setUpass(hash.toString());
		Integer result = personalServiceImpl.addUser(user);
		File dir = new File("C:\\Users\\longer\\git\\OAXiTongGuanLi\\src\\main\\webapp\\personal-file\\"+user.getUsername()); 
		dir.mkdirs();
		System.out.println(result);
		if(result!=0){
			return "redirect:Template/user-manage.jsp";
		}
		return "";
	}
	@RequestMapping("modifyUser")
	public String modifyUser(User user){
		user.setUid(0);
		String result = personalServiceImpl.modifyInfor(user);
		System.out.println(result);
		return "redirect:Template/user-manage.jsp";
	}
	
	@RequestMapping("deleteUser")
	@ResponseBody
	public String deleteUser(Integer nid){
		System.out.println(nid);
		String result = personalServiceImpl.delectUserInfo(nid);
		return result;
	}
	
	@RequestMapping("replaceImg")
	@ResponseBody
	public String replaceImg(MultipartFile file,Integer uid){
		System.out.println(uid);
		System.out.println(file);
		String fileName = file.getOriginalFilename();
		String prefix = fileName.substring(fileName.lastIndexOf("."));
		Date date = new Date();
		String newFileName = date.getTime()+prefix;
		String uploadPath="C:\\Users\\longer\\git\\OAXiTongGuanLi\\src\\main\\webapp\\head-portrait\\"+newFileName;//设置上传路径
		System.out.println("上传路径"+uploadPath);
		try {
			file.transferTo(new File(uploadPath));
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String result = personalServiceImpl.replaceUserImg(uid, newFileName);
		return result;
	}
	
	@RequestMapping("changePassWord")
	@ResponseBody
	public String changePassWord(String passWord,HttpSession session){
		User user = new User();
		System.out.println(passWord);
		Integer uid = (Integer) session.getAttribute("uid");
		String userName = (String) session.getAttribute("username");
		ByteSource credentialsSalt = ByteSource.Util.bytes(userName);
		SimpleHash hash = new SimpleHash("MD5",passWord,credentialsSalt,1024);
		System.out.println("加密之后的东西是"+hash.toString());
		user.setUid(uid);
		user.setUpass(hash.toString());		
		String result = personalServiceImpl.changePassWord(user);
		return result;
	}
	
	@RequestMapping("login")
	public String login(User user,HttpServletRequest request){
		Subject subject = SecurityUtils.getSubject();
		System.out.println("username"+user.getUsername()+"upass"+user.getUpass());
		ByteSource credentialsSalt = ByteSource.Util.bytes(user.getUsername());
		
		SimpleHash hash = new SimpleHash("MD5",user.getUpass(),credentialsSalt,1024);
		System.out.println(hash.toString());
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),hash.toString());
		try {
			subject.login(token);
			
			return "redirect:index.jsp";
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			return "redirect:login.jsp";
		}	
	}
	
	@RequestMapping("logout")
	public String logout(HttpServletRequest request){
		request.getSession().invalidate();
		return "login";
	}
	
/**
 * 修改别人的密码
 */
	@RequestMapping("changeOtherPass")
	@ResponseBody
	public String changeOtherPass(String userName,String pass){
		System.out.println("------------"+userName);
		System.out.println("********"+pass);
		ByteSource credentialsSalt = ByteSource.Util.bytes(userName);
		SimpleHash hash = new SimpleHash("MD5",pass,credentialsSalt,1024);
		System.out.println("加密之后的密码是"+hash.toString());
		User user = new User();
		user.setUsername(userName);
		user.setUpass(hash.toString());
		String result = personalServiceImpl.changeOtherPass(user);
		System.out.println(result);
		return result;
	}
	
}





